//
//  HYOUSortDetailViewController.m
//  BestGift
//
//  Created by 温晓宇 on 16/1/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HYOUSortDetailViewController.h"
#import "HYOUSortDetailTableView.h"
#import "HYOUWebViewController.h"
@interface HYOUSortDetailViewController ()<HYOUSortDetailTableViewDelegate>

@property(nonatomic, retain)HYOUSortDetailTableView *myTableView;

@property(nonatomic, retain) NSMutableArray *allArray;

@property(nonatomic,assign)NSInteger nextPage;//记录下一页
@property(nonatomic,assign)BOOL isUpLoading;//标记上拉操作还是下拉操作，yes为上拉

@property(nonatomic, copy) NSString *urlStr;

@end

@implementation HYOUSortDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.myTableView = [[HYOUSortDetailTableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    [self.myTableView.myTableView reloadData];
    self.myTableView.delegate = self;
    
    self.isUpLoading = NO;
    
    // 2.集成上拉下拉功能
    [self addHeader];
    [self addFooter];
    
    self.allArray = [NSMutableArray array];
    
    
    
}
-(void)showSortDetailWeb:(NSString *)url Image:(NSString *)image title:(NSString *)title{
    HYOUWebViewController *web = [[HYOUWebViewController alloc] init];
    web.url = url;
    web.name = title;
    web.image = image;
    [self.navigationController pushViewController:web animated:YES];
}
#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    
    __block HYOUSortDetailViewController *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    
    // 添加下拉刷新头部控件
    [self.myTableView.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.nextPage = 0;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        self.urlStr = self.url;
        [vc GetPersonList:vc.nextPage];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.myTableView.myTableView headerBeginRefreshing];
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block HYOUSortDetailViewController *vc = self;
    // 添加上拉刷新尾部控件
    [self.myTableView.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.nextPage += 20;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc GetPersonList:vc.nextPage];//请求数据
        
    }];
}





#pragma mark --使用AFN进行数据请求
-(void)GetPersonList:(NSInteger)nextCursor
{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    //****参数cursor的值: 第一页默认为0，下一页就是请求下来数据中next_cursor的值
    //****参数count 的值: 是指定一页显示多少条数据
//    static nextCursor += 20;
    self.urlStr = self.url;
    if (self.urlStr.length == 90 || self.urlStr.length == 89 || self.urlStr.length == 88) {
        NSString *next = [NSString stringWithFormat:@"%ld",(long)nextCursor];
        self.urlStr = [self.urlStr stringByReplacingCharactersInRange:NSMakeRange(self.urlStr.length - 1, 1) withString:next];
        
    }else{
        NSString *next = [NSString stringWithFormat:@"%ld",(long)nextCursor];
        self.urlStr = [self.urlStr stringByReplacingCharactersInRange:NSMakeRange(self.urlStr.length - 2, 2) withString:next];
    }
    NSLog(@"%@",self.urlStr);
//    NSLog(@"%ld",self.urlStr.length);
//    NSString *url_string = [NSString stringWithFormat:@"http://watch-cdn.idailywatch.com/api/list/video/zh-hans?page=%ld&ver=iphone&app_ver=9",nextCursor];
    
//    NSLog(@" page = %ld\n url = %@", self.nextPage, url_string);
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:self.urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        
        if (self.isUpLoading == NO) {
            [self.allArray removeAllObjects];
            self.urlStr = self.url;
        }
        
                        NSMutableDictionary *bigDic = responseObject;
                        NSMutableDictionary *dic = [bigDic objectForKey:@"data"];
                        NSMutableArray *items = [dic objectForKey:@"items"];
                        for (NSDictionary *dic in items) {
                            HYOUOneCellModel *model = [[HYOUOneCellModel alloc]init];;
                            [model setValuesForKeysWithDictionary:dic];
                            [self.allArray addObject:model];
                            [model release];
                        }
                self.myTableView.bigArray = self.allArray;
        
        [self.myTableView.myTableView footerEndRefreshing];
        [self.myTableView.myTableView headerEndRefreshing];
        [self.myTableView.myTableView reloadData];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
}
//-(void)getGiftList{
//    [AppTools finalAFNWithURLString:self.url parameters:nil successAFNBlock:^(id resuposeObject) {
//        
//        if (self.isUpLoading == NO) {
//            //说明是下拉，就要清空数组中的数据
//            [self.allArray removeAllObjects];
//        }
//        NSMutableDictionary *bigDic = resuposeObject;
//        NSMutableDictionary *dic = [bigDic objectForKey:@"data"];
//        NSMutableArray *items = [dic objectForKey:@"items"];
//        for (NSDictionary *dic in items) {
//            HYOUOneCellModel *model = [[HYOUOneCellModel alloc]init];;
//            [model setValuesForKeysWithDictionary:dic];
//            [self.allArray addObject:model];
//            [model release];
//        }
//        self.myTableView.bigArray = self.allArray;
//        NSLog(@"%@ , %ld", self.url, self.url.length);
////        [self.myTableView.myTableView footerEndRefreshing];
//        [self.myTableView.myTableView headerEndRefreshing];
//        [self.myTableView.myTableView reloadData];
//        NSLog(@"%ld",self.allArray.count);
//    }];
//}
//-(void)setAllArray:(NSMutableArray *)allArray{
//    if (_allArray != allArray) {
//        [_allArray release];
//        _allArray = [allArray retain];
//    }
//    NSLog(@"%@",self.allArray);
//    self.myTableView.bigArray = self.allArray;
//    [self.myTableView.myTableView reloadData];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
